package net.code.btalk;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;

import net.rim.device.api.system.Alert;
import net.rim.device.api.system.LED;

public class Notification {
	
	public static InputStream midStream;
	public static BTalk btalk;
	
	public static void newMessage(String from, boolean isCurrent) {
		if (!btalk.isForeground())
			btalk.requestForeground();
		
		if (isCurrent) {
			if (SavedData.vibForCurrent) {
				Alert.startVibrate(300);
			}
			
			if (SavedData.ringForCurrent) {
//				InputStream in = from.getClass().getResourceAsStream("/mid/new_msg.mid");
				try {
					midStream.reset();
					Manager.createPlayer(midStream, "audio/mid").start();
				} catch (MediaException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			
		// Other buddy
		} else {
			if (SavedData.vibForOther) {
				Alert.startVibrate(500);
			}
			
			if (SavedData.ringForOther) {
//				InputStream in = from.getClass().getResourceAsStream("/mid/new_msg.mid");
				try {
					midStream.reset();
					Manager.createPlayer(midStream, "audio/mid").start();
				} catch (MediaException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
			
			if (SavedData.ledForOther) {
				LED.setConfiguration(LED.LED_TYPE_STATUS, 500, 500, LED.BRIGHTNESS_50);
				LED.setState(LED.LED_TYPE_STATUS, LED.STATE_BLINKING);
				
			}
			
		}
	}
	
	public static void clearNotification() {
		
		if (SavedData.ledForOther) {
			LED.setState(LED.STATE_OFF);
		}
		
	}

}
